package com.lucky.auth_boot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lucky.auth_boot.exception.BusinessException;
import com.lucky.auth_boot.mapper.SystemDictDataMapper;
import com.lucky.auth_boot.mapper.SystemDictMapper;
import com.lucky.auth_boot.pojo.dto.SystemDictDTO;
import com.lucky.auth_boot.pojo.dto.SystemDictDataDTO;
import com.lucky.auth_boot.pojo.dto.UserDTO;
import com.lucky.auth_boot.pojo.po.SystemDictDataPO;
import com.lucky.auth_boot.pojo.po.SystemDictPO;
import com.lucky.auth_boot.pojo.vo.SystemDictDataVO;
import com.lucky.auth_boot.pojo.vo.SystemDictVO;
import com.lucky.auth_boot.service.SystemDictService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class SystemDictServiceImpl implements SystemDictService {
    @Resource
    private SystemDictMapper systemDictMapper;

    @Resource
    private SystemDictDataMapper systemDictDataMapper;

    @Override
    public Page<SystemDictVO> querySystemDictList(Pageable pageable, SystemDictDTO systemDictDTO) {
        Page<SystemDictVO> page = new Page<>(pageable.getPageNumber(), pageable.getPageSize());
        return systemDictMapper.querySystemDictList(page, systemDictDTO);
    }

    @Override
    public Page<SystemDictDataVO> querySystemDictDataList(Pageable pageable, SystemDictDataDTO systemDictDataDTO) {
        Page<SystemDictDataVO> page = new Page<>(pageable.getPageNumber(), pageable.getPageSize());
        return systemDictDataMapper.querySystemDictDataList(page, systemDictDataDTO);
    }

    @Override
    public SystemDictPO querySystemDictById(Integer id) {
        QueryWrapper<SystemDictPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        SystemDictPO systemDictPO = systemDictMapper.selectOne(queryWrapper);
        if (systemDictPO != null) {
            systemDictPO.setCreator(null);
        }
        return systemDictPO;
    }

    @Override
    public SystemDictDataPO querySystemDictDataById(Integer id) {
        QueryWrapper<SystemDictDataPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dict_code", id);
        SystemDictDataPO systemDictDataPO = systemDictDataMapper.selectOne(queryWrapper);
        if (systemDictDataPO != null) {
            systemDictDataPO.setCreator(null);
        }
        return systemDictDataPO;
    }

    @Override
    public Integer putSystemDictById(SystemDictDTO systemDictDTO) {
        try {
            UserDTO userDTO = (UserDTO) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

            SystemDictPO systemDictPO = new SystemDictPO();
            BeanUtils.copyProperties(systemDictDTO, systemDictPO);
            systemDictPO.setUpdater(userDTO.getId());
            systemDictPO.setUpdateTime(new Date());
            systemDictPO.setCreator(null);
            systemDictPO.setCreateTime(null);


            QueryWrapper<SystemDictPO> wrapper = new QueryWrapper<>();
            wrapper.eq("id", systemDictDTO.getId());
            return systemDictMapper.update(systemDictPO, wrapper);
        } catch (Exception e) {
            throw new BusinessException(500, "操作失败");
        }
    }

    @Override
    public Integer putSystemDictDataById(SystemDictDataDTO systemDictDataDTO) {
        try {
            UserDTO userDTO = (UserDTO) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

            SystemDictDataPO systemDictDataPO = new SystemDictDataPO();
            BeanUtils.copyProperties(systemDictDataDTO, systemDictDataPO);
            systemDictDataPO.setUpdater(userDTO.getId());
            systemDictDataPO.setUpdateTime(new Date());
            systemDictDataPO.setCreator(null);
            systemDictDataPO.setCreateTime(null);
            systemDictDataPO.setDictType(null);

            QueryWrapper<SystemDictDataPO> wrapper = new QueryWrapper<>();
            wrapper.eq("dict_code", systemDictDataDTO.getDictCode());
            return systemDictDataMapper.update(systemDictDataPO, wrapper);
        } catch (Exception e) {
            throw new BusinessException(500, "操作失败");
        }
    }

    @Override
    public Integer postSystemDictAdd(SystemDictDTO systemDictDTO) {
        UserDTO userDTO = (UserDTO) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        SystemDictPO systemDictPO = new SystemDictPO();
        BeanUtils.copyProperties(systemDictDTO, systemDictPO);
        systemDictPO.setUpdater(null);
        systemDictPO.setUpdateTime(null);
        systemDictPO.setCreator(userDTO.getId());
        systemDictPO.setCreateTime(new Date());
        systemDictPO.setId(null);

        QueryWrapper<SystemDictPO> wrapper = new QueryWrapper<>();
        wrapper.eq("dict_type", systemDictPO.getDictType());
        if (systemDictMapper.selectCount(wrapper) > 0) {
            throw new BusinessException("该字典类型已存在");
        }

        try {
            return systemDictMapper.insert(systemDictPO);
        } catch (Exception e) {
            throw new BusinessException(500, "操作失败");
        }
    }

    @Override
    public List querySystemDictDataListAll(String dictType) {
        QueryWrapper<SystemDictPO> queryWrapperDict = new QueryWrapper<>();
        queryWrapperDict.eq("dict_type", dictType);
        queryWrapperDict.eq("status", 1);
        Long count = systemDictMapper.selectCount(queryWrapperDict);

        // 判断是否有数据
        if (count == null || count == 0) {
            // 没有数据，直接返回空列表
            return new ArrayList<>();
        }

        QueryWrapper<SystemDictDataPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dict_type", dictType);
        queryWrapper.eq("status", 1);
        List<SystemDictDataVO> list = new ArrayList<SystemDictDataVO>();
        for (SystemDictDataPO systemDictDataPO : systemDictDataMapper.selectList(queryWrapper)) {
            SystemDictDataVO vo = new SystemDictDataVO();
            BeanUtils.copyProperties(systemDictDataPO, vo);
            try {
                // 将 dictValue 转换为整数类型
                vo.setDictValueNum(Integer.parseInt(systemDictDataPO.getDictValue()));
            } catch (NumberFormatException e) {
                // 处理转换异常，这里可以根据实际情况进行处理
                vo.setDictValueNum(null);
            }
            list.add(vo);
        }
        return list;
    }
}
